(ns econews.util)


(defn contains-val?
  [coll val]
  (reduce #(if (= val %2) (reduced true) %1) false coll))

(defn distinct-by [f coll]
  (let [step (fn step [xs seen]
               (lazy-seq
                 ((fn [[x :as xs] seen]
                    (when-let [s (seq xs)]
                      (let [fx (f x)]
                        (if (contains? seen fx)
                          (recur (rest s) seen)
                          (cons x (step (rest s) (conj seen fx)))))))
                   xs seen)))]
    (step coll #{})))

(defn average
  [numbers]
  (/ (apply + numbers) (count numbers)))
